package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zjx on 2018
 */

public class UserModifySigntextActivity extends AppCompatActivity {
    public Dialog loadingdialog = null;
    @BindView(R.id.backbt)
    RelativeLayout backbt;

    @BindView(R.id.savebt)
    RelativeLayout savebt;
    @BindView(R.id.rl_title_area)
    RelativeLayout rlTitleArea;
    @BindView(R.id.right)
    RelativeLayout right;
    @BindView(R.id.nicheng_edit)
    EditText nichengEdit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.initStatus(this, R.color.black);
        setContentView(R.layout.activity_modifysigntext);
        ButterKnife.bind(this);
        initComponent();
    }

    private void initComponent() {
        this.loadingdialog = new Dialog(this);
        this.loadingdialog.setContentView(R.layout.dialog_loading);
        this.loadingdialog.setCanceledOnTouchOutside(false);
        this.loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @OnClick(value = {R.id.backbt, R.id.savebt, R.id.right})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.savebt:
                if (TextUtils.isEmpty(this.nichengEdit.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "签名不能为空", 0).show();
                    return;
                }
                usermodifySigntext(this.nichengEdit.getText().toString().trim());
                break;
            case R.id.right:
                nichengEdit.getText().clear();
                break;


        }
    }

    public void usermodifySigntext(final String userSigntext) {
        this.loadingdialog.show();
        List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if (userList == null || userList.size() == 0) {
            return;
        }
        Map<String, String> map = new HashMap();
        map.put("userId", userList.get(0).getUserId() + "");
        map.put("userSigntext", userSigntext);
        Xutils.POST(HttpUrl.usermodifysigntext, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                UserModifySigntextActivity.this.loadingdialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    switch (jsonObject.getInt("state")) {
                        case HttpCode.SUCCESS:
                            Intent data = new Intent();
                            data.putExtra("data", userSigntext);
                            setResult(RESULT_OK, data);
                            finish();
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                UserModifySigntextActivity.this.loadingdialog.dismiss();
                Toast.makeText(UserModifySigntextActivity.this.getApplicationContext(), getResources().getString(R.string.please_wait), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
